home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / KmdOps / snapshot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-30  |  8.1 KB  |  315 lines

  1. /* COPYRIGHT  NOTICE :
  2.  * Copyright 1986 Eric Jul.  May not be used for any
  3.  * purpose without written permission from the author.
  4.  */
  5.  
  6. /* File:        snapshot.c      Eric Jul, 1986-05-03  */
  7.  
  8. /*
  9.  * $Header$
  10.  * INTERFACE:    See usage printout.
  11.  *
  12.  * FUNCTION:    sends snapshot requests to the kernel
  13.  *
  14.  * IMPORTS:    loads, see #include
  15.  *
  16.  * EXPORTS:    Nothing.
  17.  *
  18.  * DESIGN:    Send snapshot message to process and then
  19.  *              repeatedly receive and print data until stop snapshot
  20.  *              message arrives.
  21.  *
  22.  * $Log$
  23.  * 1983-06-08   Initial implementation, Eric Jul.
  24.  * 1985-03-04   Eric Jul
  25.  * 1986-05-03   Modified to work with 4.2bsd sockets.
  26.  */
  27.  
  28. #undef integer
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <stdio.h>
  33.  
  34. #define mSUCCESS(X) (((X) & 7) == 1)
  35.  
  36. typedef int KKStatus;
  37.  
  38. #define MAXMESSAGESIZE        1000
  39. #include "Kernel/h/emPorts.h"
  40. #include "Kernel/h/kmdDefs.h"
  41.  
  42. #include <netdb.h>
  43. #include <signal.h>
  44.  
  45. #define endcase break
  46.  
  47. char *snapshotname, *snapshotstring;
  48. int                  talkSock;
  49. int                  pid = 0;
  50. int                  prodTime = 1;
  51. int                  MType, MInt;
  52. char                 MString[5000];
  53. struct sockaddr_in   destSockDescr = {AF_INET};
  54.  
  55. /* Define signal handlers */
  56.  
  57. void PipeHandler(sig)
  58. int sig;
  59. {
  60.   printf("SIGPIPE>>> The connection to the process broke.\nTerminating.\n");
  61.   exit(sig);
  62. }
  63.  
  64. void TerminationHandler(sig)
  65. int sig;
  66.  
  67.     if (sig != 0) {
  68.     printf("Terminating snapshot due to signal %d.\n", sig);
  69.     } else {
  70.     printf("Snapshot done.\n");
  71.     }
  72.  
  73.     /* Important to tell kernel that we are bye, bye ... */
  74.     KMDSend(talkSock, KMDCLASTMSG, 0, "Done");
  75.     if (shutdown(talkSock, 1) < 0) {
  76.     perror("shutdown1");
  77.         close(talkSock);
  78.     exit(sig);
  79.     }
  80.     
  81.     while(mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString)));
  82.     close(talkSock);
  83.     exit(sig);
  84. };
  85.  
  86.  
  87. void Prodder()
  88. /* Prods the kernel by sending a SIGURG signal.
  89.    Exponential backoff.
  90.  */
  91. {
  92. /*    kill(pid, SIGURG); */
  93.     prodTime += prodTime;
  94.     alarm(prodTime);
  95. }
  96.  
  97.  
  98. main(argc, argv)
  99. int         argc;
  100. char        *argv[];
  101. {
  102.   int               OK;
  103.   char              snapbuf[500];
  104.   static int        i, EPLNN, SValue;
  105.   static int        pidGiven = 0, machineNameGiven = 0;
  106.   KKStatus          kstat;
  107.   char              myHostName[100];
  108.   struct hostent   *myHost;
  109.  
  110.     snapshotname = "Menu";           /* default */
  111.     snapshotstring = (char *) 0;
  112.     SValue = 0;                      /* default */
  113.     EPLNN = 8;                       /* default */
  114.     
  115.         if (argc == 2) 
  116.             if (!strcmp(argv[1], "usage")) {
  117.             printf("Usage: snapshot [-m <machine name>] [-N <LNN> | -X <hexLNN> | -P <pid>] [-n <snapname>] [-{d|h} <parameter value> | -S <string>\n");
  118.                 exit(0);
  119.             };
  120.  
  121.     /*
  122.      * Parse command line options
  123.      */
  124.     while (argc > 1 && argv[1][0] == '-') {
  125.         switch (argv[1][1]) {
  126.         case 'N':        /* Set logical node number */
  127.             if (argv[1][2] == '\0') {
  128.                 EPLNN = atoi(argv[2]);
  129.                 argc--;
  130.                 argv++;
  131.             } else
  132.                 EPLNN = atoi(&argv[1][2]);
  133.                         
  134.             printf("warning: -N ignored.\n");
  135.             break;
  136.         case 'X':        /* Set logical node number (hex) */
  137.                         if (argv[1][2] == '\0') {
  138.                                 sscanf(argv[2], "%x", &EPLNN);
  139.                                 argc--;
  140.                                 argv++;
  141.                         } else
  142.                                 sscanf(&argv[1][2], "%x", &EPLNN);
  143.             printf("warning: -X ignored.\n");
  144.             break;
  145.         case 'D':
  146.             KMDTest = 1;
  147.             break;
  148.         case 'd':
  149.             if (argv[1][2] == '\0') {
  150.                 SValue = atoi(argv[2]);
  151.                 argc--;
  152.                 argv++;
  153.             } else
  154.                 SValue = atoi(&argv[1][2]);
  155.             break;
  156.         case 'P':       /* Set pid, cancel LNN usage. */
  157.             if (argv[1][2] == '\0') {
  158.                 pid = atoi(argv[2]);
  159.                 argc--;
  160.                 argv++;
  161.             } else
  162.                 pid = atoi(&argv[1][2]);
  163.                         pidGiven = 1;
  164.             printf("warning: -P ignored.\n");
  165.             break;
  166.                 case 'h':
  167.                         if (argv[1][2] == '\0') {
  168.                                 sscanf(argv[2], "%x", &SValue);
  169.                                 argc--;
  170.                                 argv++;
  171.                         } else
  172.                                 sscanf(&argv[1][2], "%x", &SValue);
  173.                         break;
  174.                 case 'n':
  175.                         snapshotname = argv[2];
  176.                         argc--;
  177.                         argv++;
  178.                         break;
  179.                 case 'S':
  180.                         snapshotstring = argv[2];
  181.                         argc--;
  182.                         argv++;
  183.                         break;
  184.                 case 'M':
  185.                 case 'm':
  186.                         strcpy(myHostName, argv[2]);
  187.                         argc--;
  188.                         argv++;
  189.             machineNameGiven ++;
  190.                         break;
  191.                 default:
  192.                         printf("Warning: Unknown option: %c", argv[1][2]);
  193.                         break;
  194.  
  195.         }
  196.         argc--;
  197.         argv++;
  198.     }
  199.  
  200.     
  201.     /* Set termination handler. */
  202.     signal(SIGHUP, TerminationHandler);
  203.     signal(SIGQUIT, TerminationHandler);
  204.     signal(SIGINT, TerminationHandler);
  205.     signal(SIGBUS, TerminationHandler);
  206.     signal(SIGSEGV, TerminationHandler);
  207.     signal(SIGTERM, TerminationHandler);
  208.     signal(SIGPIPE, PipeHandler);
  209.     signal(SIGALRM, Prodder);
  210.     
  211.     talkSock = socket(AF_INET, SOCK_STREAM, 0, 0);
  212.     if (talkSock < 0) {
  213.                 perror("snapshot: socket");
  214.                 exit(1);
  215.     }
  216.  
  217.     if (! machineNameGiven) {
  218.     if (gethostname(myHostName, 100) < 0) {
  219.         perror("snapshot: gethostname");
  220.         exit(1);
  221.     }
  222.     }
  223.     myHost = gethostbyname(myHostName);
  224.     if (myHost == NULL) {
  225.             perror("snapshot: gethostbyname");
  226.             exit(1);
  227.     }
  228.  
  229.     bcopy(myHost->h_addr, &destSockDescr.sin_addr, myHost->h_length);
  230.  
  231.     {
  232.       char *emplanename;
  233.       int emPlane = 0;
  234.       int emPort = EMKERNELDEFAULTPORTNUMBER;
  235.       struct servent   *myService;
  236.       emplanename = (char *) getenv("EMPLANE");
  237.       if (emplanename != NULL) emPlane = atoi(emplanename);
  238. #ifdef DIKU
  239.       myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
  240.       if(myService)
  241.     emPort = ntohs(myService->s_port);
  242. #endif
  243.       destSockDescr.sin_port = htons(emPort + emPlane*4 + 1);
  244.     }
  245.  
  246.     if (connect(talkSock, &destSockDescr, sizeof(destSockDescr)) == -1) {
  247.                 perror("snapshot: connect");
  248.                 exit(1);
  249.     }
  250.  
  251.  
  252.     if ( KMDTest )
  253.         printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
  254.     
  255.     KMDReceive(talkSock, &MType, &MInt, MString);
  256.     printf("%s", MString);
  257.  
  258.     /* Start snapshot.*/
  259.     sprintf(snapbuf, "%s %s", snapshotname, snapshotstring);
  260.     kstat = KMDSend(talkSock, KMDCSNAPSHOT, SValue, snapbuf);
  261.  
  262.     if (! mSUCCESS( kstat ) ) {
  263.         printf("snapshot: Send start snapshot msg failure, kstat = 0x%08x\n",
  264.         kstat);
  265.         exit(1);
  266.     };
  267.     
  268.     printf("Calling %s on %s, param: %d (0x%02x)\n",
  269.         snapshotname, myHostName, SValue, SValue);
  270.     
  271.     fflush(stdout);
  272.     OK = 1;
  273.     while (OK==1) {
  274.         if(!mSUCCESS(kstat = KMDReceive(talkSock, &MType, &MInt, MString))){
  275.         printf("Premature snapshot termination\n");
  276.         perror("recv");
  277.             printf("Kernel status 0x%06x\n");
  278.         TerminationHandler(0);
  279.     };
  280.     
  281.         if ( KMDTest ){
  282.             printf("Msg from: %d, (%d, %d, %s)\n", talkSock,
  283.                 MType, MInt, MString),
  284.             fflush(stdout);
  285.     };
  286.         switch ( MType ) {
  287.         
  288.         case KMDCDATA:
  289.             printf("%s", MString);
  290.             fflush(stdout);
  291.         endcase;
  292.         
  293.         case KMDCERRMSG: /* Error message */
  294.             printf("snapshot: %s\n", MString);
  295.             fflush(stdout);
  296.             OK = 0;         /* quit */
  297.         endcase;
  298.         
  299.         case KMDCLASTMSG:
  300.             printf("%s", MString);
  301.             OK = 0;         /* quit. */
  302.         endcase;
  303.  
  304.         default:
  305.             printf("snapshot: Warning, bad msgtype: %d - ignored.\n", MType);
  306.         };
  307.     }
  308.     TerminationHandler(0);        
  309. }
  310.  
  311. /*  C O P Y R I G H T   N O T I C E :                                     */
  312. /* Copyright 1986 Eric Jul.  May not be used for any               */
  313. /* purpose without written permission from the author.              */
  314.